<template>
  <div class="dialog">
    <el-dialog width="45%" title="上传头像" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="当前手机号：" prop="oldMobile">
          <el-col :span="18">
            <el-input v-model="form.oldMobile" v-int placeholder="请输入" maxlength="11" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="验证码：" prop="smsCode">
          <el-col :span="18">
            <el-input v-model="form.smsCode" placeholder="请输入验证码">
              <span v-if="showTime" slot="append" class="send-code">{{ durationTime }}s</span>
              <el-button v-else slot="append" class="send-code" @click="getCode">获取验证码</el-button>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="新手机号：" prop="newMobile">
          <el-col :span="18">
            <el-input v-model="form.newMobile" v-int placeholder="请输入" maxlength="11" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="确认手机号：" prop="newMobileConfirm">
          <el-col :span="18">
            <el-input v-model="form.newMobileConfirm" v-int placeholder="请输入" maxlength="11" clearable />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { sendSms } from '@/api/login'
import { mobileReg } from '@/utils/const'

export default {
  name: 'Phone',

  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value && value !== this.form.newMobile) {
        callback(new Error('两次输入的手机号不一致'))
      } else {
        callback()
      }
    }
    return {
      showTime: false,
      durationTime: 59,
      timer: '',
      form: {
        oldMobile: '',
        smsCode: '',
        newMobile: '',
        newMobileConfirm: ''
      },
      rules: {
        oldMobile: [
          { required: true, message: '请输入当前手机号', trigger: ['blur'] },
          { pattern: mobileReg, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        smsCode: [
          { required: true, message: '请输入验证码', trigger: ['blur'] }
        ],
        newMobile: [
          { required: true, message: '请输入当前手机号', trigger: ['blur'] },
          { pattern: mobileReg, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        newMobileConfirm: [
          { required: true, message: '请输入当前手机号', trigger: ['blur'] },
          { validator: validatePass2, trigger: 'blur' }
        ]

      }
    }
  },

  methods: {

    // 获取验证码
    getCode() {
      const data = {
        mobile: this.form.oldMobile,
        smsType: 'editPhoneBucket'
      }
      sendSms(data).then(() => {
        this.timeStart()
      }).catch(() => {})
    },

    // 倒计时
    timeStart() {
      this.showTime = true
      this.timer = setInterval(() => {
        while (this.durationTime === 1) {
          this.showTime = false
          this.durationTime = 59
          clearInterval(this.timer)
          return
        }
        this.durationTime--
      }, 1000)
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确认
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$emit('confirm', this.form)
        } else {
          return false
        }
      })
    },

    // 上传图片处理
    uploadHandle(payload, name) {
      this.form[name] = payload.fileList[0]?.url
    }
  }

}
</script>

<style lang="scss" scoped></style>
